﻿using MultLanguage.ConfigFile;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using LogLib;

namespace MultLanguage
{
    /// <summary>
    /// 应用程序多语言服务类
    /// </summary>
    public static class MLService
    {
        private static MultLanguageService service = null;

        public const string DefaultCode = "zh-CN";

        public static string LanguageCode = "zh-CN";

        private static object _lock = new object();

        static MLService()
        {
            service = MultLanguageService.GetInstance();
        }

        public static void Refresh(string code)
        {
            lock (_lock)
            {
                if (code != LanguageCode)
                {
                    LanguageCode = code;
                    Log.WriteAsync("语言服务器代码已经切换为: " + code);
                }
            }
        }

        public static string Translate(string key)
        {
            lock (_lock)
            {
                if (LanguageCode.Equals("zh-CN"))
                {
                    return key;
                }
                if (service == null || !MultLanguageService.InitSuccessed)
                {
                    return key;
                }
                return service[LanguageCode, key];
            }
        }
    }
}
